package com.study.zouchao.superproject.qustionadd.view;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

import com.study.zouchao.superproject.R;
import com.study.zouchao.superproject.base_zou.MyBaseFragment;
import com.study.zouchao.superproject.qustionadd.contract.IQuestionAddContract;
import com.study.zouchao.superproject.qustionadd.presenter.QuestionAddPresenter;
import com.study.zouchao.superproject.util.DialogUtil;
import com.study.zouchao.superproject.util.LogLongUtil;
import com.study.zouchao.superproject.util.StringUtils;
import com.study.zouchao.superproject.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/4/25.
 */

public class QuestionAddFragment extends MyBaseFragment implements IQuestionAddContract.IQuestionAddView,  DialogInterface.OnCancelListener {

    private static final String TAG = "QuestionAddF";


    @BindView(R.id.id_editor)
    EditText mEtContent;

    @BindView(R.id.id_et_title)
    EditText mEtTitle;



    @BindView(R.id.id_edit_contact)
    EditText mEtContact;

    @BindView(R.id.id_edit_reward_price)
    EditText mEtRewardPrice;

    @BindView(R.id.id_edit_reward)
    EditText mEtReward;

    //是否上传提示框
    private AlertDialog mAlertDialog;

    private IQuestionAddContract.IQuestionAddPresenter mPresenter;

    private ProgressDialog mProgressDialog;

    public static final String KEY_TAG_ID = "KEY_TAG_ID";

    public QuestionAddFragment() {}


    /*
     *
     * 生命周期
     *
     */
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
    }


    private String getTagId() {
        Log.d(TAG, "能恢复吗:tagId:"+getActivity().getIntent().getStringExtra(KEY_TAG_ID));
        return getActivity().getIntent().getStringExtra(KEY_TAG_ID);
    }

    private void init() {
        mProgressDialog = new ProgressDialog(getActivity());
        mProgressDialog.setCancelable(false);
        mProgressDialog.setIcon(R.drawable.ic_launcher);
        mProgressDialog.setTitle("正在上传...");
        mProgressDialog.setCancelable(true);
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.setOnCancelListener(this);
        mPresenter = new QuestionAddPresenter(getActivity(), this);
        mPresenter.setTagId(getTagId());

        Toolbar toolbar = (Toolbar) getCurrentFragmentRootView().findViewById(R.id.toolbar);
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        setHasOptionsMenu(true);
        mEtContent.setSelection(0);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
        inflater.inflate(R.menu.menu_my_answer_write, menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.id_menu_commit :
                Log.i("", "");
                mPresenter.prepareUploadText(mEtTitle.getText().toString(), mEtContent.getText().toString().trim(),
                        mEtReward.getText().toString(), mEtRewardPrice.getText().toString(), mEtContact.getText().toString());
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 拍照/选择图片 完成后回调
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        //这个super不能落下 否则可能回调不了
        super.onActivityResult(requestCode, resultCode, data);
        //处理resultCode
        mPresenter.judgeActivityResult(requestCode, resultCode, data);
    }


    /*
     *
     * View
     *
     */
    /**
     * 当点击头像时弹出一个选项框选择是从哪里取图片
     */
    public void showChoosePicDialog() {
        String title = "设置头像";
        String[] items = {"选择本地图片", "拍照"};
        DialogUtil.showItemAlertDialog(getContext(), title, items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mPresenter.clickChoosePicDialog(which);
            }
        });
    }

    @Override
    public void toggleLoading(boolean isShow) {
        if (isShow && !mProgressDialog.isShowing()) mProgressDialog.show();
        if (!isShow && mProgressDialog.isShowing()) mProgressDialog.dismiss();
    }

    @Override
    public void successUpdateImg(String serverUrl) {
        LogLongUtil.logD("上传陈宫为什么没反应", serverUrl);
//        mEditor.insertImageAndBrAndCenter(serverUrl, serverUrl, "auto", "100%", true);
//        LogLongUtil.logD("额。。",mEditor.getHtml());
    }

    @Override
    public void showTitleError(String errorMsg) {mEtTitle.setError(errorMsg);}

    @Override
    public void showContentError(String errorMsg) {
        ToastUtils.showShort(getActivity().getApplicationContext(), errorMsg);}

    @Override
    public void finishCurrentActivity() {
        if (getActivity() != null)  getActivity().finish();
    }

    @Override
    public void gotoActivity(Class targetActivity, String K, String V) {}

    private void showAlertDialog() {
        if (mAlertDialog!=null && !mAlertDialog.isShowing()) {
            mAlertDialog.show();
        }
    }

    private void dismissAlertDialog() {
        if (mAlertDialog != null && mAlertDialog.isShowing()) {
            mAlertDialog.dismiss();
            mAlertDialog = null;
        }
//        mProgressDialog = null;
    }

    /*
     *
     */


    @Override
    public void showEnsureAlertDialog() {
        //显示考试结束对话框
        mAlertDialog = mPresenter.getEnsureUploadAlertDialog("编辑完成", "确认提交？");
        //显示考试结束对话框
        showAlertDialog();
    }

    @Override
    public void startUploadText() {
        mPresenter.startUploadText(mEtTitle.getText().toString(), mEtContent.getText().toString().trim(),
                mEtReward.getText().toString(), mEtRewardPrice.getText().toString(), mEtContact.getText().toString());
    }

    @Override
    public void showSnackBar(String title, int showLength) {
        Snackbar.make(getCurrentFragmentRootView(), title, showLength)
                .show();
    }


    @Override
    public void onDestroy() {
        dismissAlertDialog();
        super.onDestroy();
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        mPresenter.cancelUploadProgressDialog();
    }

    @Override
    protected View getLoadingTargetView() {return getCurrentFragmentRootView().findViewById(R.id.id_loading_fragment_answer_writer);}

    @Override
    protected int getFragmentLayoutID() {
        return R.layout.fragment_question_add;
    }


}
